package com.zhku.meeting.util;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zhku.meeting.enums.CodeEnum;
import lombok.Data;
import java.io.Serializable;
import java.util.Objects;


@Data
public class ResultVO<T> implements Serializable {

    private static final long serialVersionUID = 8960474786737581150L;

    public static final String SUCCESS = "success";

    public static final String FAIL = "fail";

    public static final String FALLBACK = "fallback";

    /**
     * 错误码
     */
    private String code;
    /**
     * 提示信息
     */
    private String resultDesc;

    /**
     * 具体内容
     */
    private T data;

    public static <T> ResultVO<T> create(String code, String resultDesc) {
        return new ResultVO(code, resultDesc, null);
    }

    public static <T> ResultVO<T> create(String code, String resultDesc, T data) {
        return new ResultVO(code, resultDesc, data);
    }

    public static <T> ResultVO<T> successData(String message, T data) {
        return create(CodeEnum.CODE_100.code(), message, data);
    }

    public static <T> ResultVO<T> loginSuccess(String message, T data) {
        return create(CodeEnum.CODE_100.code(), message, data);
    }

    public static <T> ResultVO<T> successMessage(String message) {
        return successData(message, null);
    }

    public static <T> ResultVO<T> successData(T data) {
        return successData(null, data);
    }

    public static <T> ResultVO<T> success() {
        return successMessage(null);
    }

    public static <T> ResultVO<T> errorData(String message, T data) {
        return create(FAIL, message, data);
    }

    public static <T> ResultVO<T> error() {
        return errorMessage(null);
    }

    public static <T> ResultVO<T> errorData(T data) {
        return errorData(null, data);
    }

    public static <T> ResultVO<T> errorMessage(String message) {
        return errorData(message, null);
    }

    public static <T> ResultVO<T> selectiveMessage(boolean success, String successMessage, String errorMessage) {
        return success ? successMessage(successMessage) : errorMessage(errorMessage);
    }

    public static <T> ResultVO<T> selectiveMessage(boolean success, String successMessage, String errorMessage, T data) {
        return success ? successData(successMessage, data) : errorMessage(errorMessage);
    }

    public ResultVO() {
    }
    public ResultVO(String code, String resultDesc, T data) {
        this.code = code;
        this.resultDesc = resultDesc;
        this.data = data;
    }

    public boolean hasData() {
        return Objects.equals(CodeEnum.CODE_100.code(), this.code) && this.data != null;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return Objects.equals(CodeEnum.CODE_100.code(), this.code);
    }


    public static ResultVO fallbackResult() {
        return ResultVO.create(FALLBACK, null);
    }
}
